package com.ecoolex.chinadutyfree.retailer.admin.graphql;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.lang.NonNull;

import com.ecoolex.chinadutyfree.retailer.admin.core.AdminConstant;
import com.ecoolex.chinadutyfree.retailer.entity.GoodsCategory;
import com.ecoolex.chinadutyfree.retailer.entity.GoodsCategoryMarketing;
import com.ecoolex.chinadutyfree.retailer.entity.GoodsCategoryMarketing.Column;
import com.ecoolex.chinadutyfree.retailer.pojo.AddGoodsCategoryMarketingDTO;
import com.ecoolex.chinadutyfree.retailer.pojo.EditGoodsCategoryMarketingDTO;
import com.ecoolex.chinadutyfree.retailer.ws.GoodsService;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.BeanUtil;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.admin.AdminMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.annotation.AdminAuthoritativeRegister;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.google.common.collect.Lists;

@GraphQL2Module(type = Type.Admin, description = "商品分类推荐管理")
@AdminAuthoritativeRegister(parentKey = AdminConstant.AUTH_MODULE_KEY,
	key = GoodsCategoryMarketingGraphql.AUTH,
	name = "商品分类推荐管理")
public class GoodsCategoryMarketingGraphql {

	public static final String AUTH = AdminConstant.AUTH_MODULE_KEY + "-GOODS-CATEGORY-MARKETING";
	
	@Reference
	private GoodsService goodsService;
	
	@TypeField(type = GoodsCategoryMarketing.class,field = "goodsCategory" ,description = "商品分类")
	public GoodsCategory goodsCategory(@Source GoodsCategoryMarketing source) {
		return goodsService.getCategoryById(source.getCategoryId());
	}
	
	private static final String AUTH_QUERY = AUTH + "-QUERY";
	
	@QueryField(field = "goodsCategoryMarketingList", description = { "查询分类推荐列表" })
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_QUERY, name = "查询分类推荐列表")
	public Pagination<GoodsCategoryMarketing> query(String name,@NonNull Sorting[] sorts,@NonNull Page page, AdminMemberAuthority user){
		user.assertAuth(AUTH_QUERY);
		return goodsService.goodsCategoryMarketingList(name,sorts, page);
	}
	
	private static final String AUTH_GET = AUTH + "-GET";
	
	@QueryField(field = "goodsCategoryMarketing", description = { "查询分类推荐详情" })
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_GET, name = "查询分类推荐详情")
	public GoodsCategoryMarketing get(@NonNull Integer id, AdminMemberAuthority user) {
		user.assertAuth(AUTH_GET);
		return goodsService.getGoodsCategoryMarketing(id);
	}
	
	private static final String AUTH_ADD = AUTH + "-ADD";
	
	@MutationField(field = "addGoodsCategoryMarketing", description = "添加分类推荐")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_ADD, name = "添加分类推荐")
	public GoodsCategoryMarketing add(@NonNull AddGoodsCategoryMarketingDTO dto,AdminMemberAuthority user) {
		user.assertAuth(AUTH_ADD);
		return goodsService.addGoodsCategoryMarketing(dto, user.userName());
	}
	
	private static final String AUTH_EDIT = AUTH + "-EDIT";
	
	@MutationField(field = "editGoodsCategoryMarketing", description = "编辑分类推荐")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_EDIT, name = "编辑分类推荐")
	public GoodsCategoryMarketing edit(@NonNull EditGoodsCategoryMarketingDTO dto,AdminMemberAuthority user) {
		user.assertAuth(AUTH_EDIT);
		GoodsCategoryMarketing old = goodsService.getGoodsCategoryMarketing(dto.getId());
		Assert.isTrue(Check.notNull(old), ResultCode.VERIFY.build(1), "分类推荐不存在");
		BeanUtil.copy(dto,
			old,
			Lists.newArrayList(
				Column.deleted,
				Column.createUser,
				Column.createTime).stream().map(Column::getJavaProperty).toArray(String[]::new));
		return goodsService.editGoodsCategoryMarketing(old, user.userName());
	}
	
	private static final String AUTH_DEL = AUTH + "-DEL";
	
	@MutationField(field = "delGoodsCategoryMarketing", description = "删除分类推荐")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_DEL, name = "删除分类推荐")
	public boolean delete(@NonNull Integer categoryId,AdminMemberAuthority user) {
		user.assertAuth(AUTH_DEL);
		GoodsCategoryMarketing record = goodsService.getGoodsCategoryMarketing(categoryId);
		Assert.isTrue(Check.notNull(record), ResultCode.VERIFY.build(1), "分类推荐不存在");
		goodsService.deleteGoodsCategoryMarketing(record, user.userName());
		return true;
	}
}

